package cn.hh.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 订单状态随机生成
 * @Author huangh
 * @date 2021/07/27
 * @Description:
 */
@RestController("/")
public class OrderController {

    @PostMapping(value = "/info", produces = "application/json;charset=utf-8")
    public Map<String,Object> info() throws InterruptedException {
        //tring basePath = System.getProperty("user.dir");
        //System.out.println("-----" +basePath);
        //String jsonFile = basePath+ "/file/result.json";
        String jsonFile = "D:\\work\\java\\springbootw\\springboot-learn\\common-util\\file\\result.json";

        int rand = RandomUtil.randomInt(1,6);
        //int sucRand = RandomUtil.randomInt(10,26);
        int sucRand = 24; //订单一直在途状态
        System.out.println("延时----"+rand+" 成功数----"+sucRand);

        Thread.sleep(1*1000);

        Map<String,Object> result = new HashMap<String,Object>();
        String content = FileUtil.readUtf8String(jsonFile);
        result = (Map<String,Object>)JSONUtil.parseObj(content);
        if(25 ==sucRand){
            result.put("code","0");
        }
        if(24 ==sucRand){
            result.put("code","8");
        }
        result.put("sucRand",sucRand);
        /*if(true){
            throw new RuntimeException();
        }*/


        return result;
    }

    /**
     * 绑定信息
     * @return
     */
    @PostMapping(value = "/bindInfo", produces = "application/json;charset=utf-8")
    public Map<String,Object> bindInfo() {
        Map<String,Object> result = new HashMap<String,Object>();

        return result;
    }


}
